package com.aw.baselib.util

import android.content.Context
import android.text.InputType
import android.widget.EditText
import android.widget.RadioButton
import android.widget.TextView
import com.aw.baselib.R
import com.aw.baselib.base.AwBaseApplication
import com.aw.baselib.util.AwDataUtil.isEmpty
import com.aw.baselib.util.AwEditTextListener.isChangeListener

/**
 * Created by hzw
 */
object AwViewUtil {
    /**
     * @category 让EditText内的密码显示明文或显示mask符号
     */
    fun setEditTxtVisibility(et: EditText, flag: Boolean) {
        val type =
            if (flag) InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD else InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        et.inputType = type
    }

    fun setTopView(context: Context, view: RadioButton, drawableId: Int) {
        val top = context.resources.getDrawable(drawableId)
        top.setBounds(0, 0, top.minimumWidth, top.minimumHeight)
        view.setCompoundDrawables(null, top, null, null)
    }

    fun setRightView(context: Context, view: TextView, drawableId: Int) {
        val drawable = context.resources.getDrawable(drawableId)
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        view.setCompoundDrawables(null, null, drawable, null)
    }

    fun setEnableView(etList: List<EditText?>, et: EditText?, tv: TextView) {
        isChangeListener(
            et!!,
            object : AwEditTextListener.EditTextChangeListenerCallback {
                override fun result(changed: Boolean) {
                    if (changed) {
                        if (isEmpty(etList)) {
                            AwBaseApplication.instance?.resources?.getColor(R.color.white)
                                ?.let { tv.setTextColor(it) }
                            tv.background = AwBaseApplication.instance?.getDrawable(R.drawable.bg_common_btn_gray)
                            return
                        }
                        var isPass = true
                        for (item in etList) {
                            if (isEmpty(item!!.text.toString())) {
                                isPass = false
                                break
                            }
                        }
                        if (isPass) {
                            AwBaseApplication.instance?.resources?.getColor(R.color.white)
                                ?.let { tv.setTextColor(it) }
                            tv.background = AwBaseApplication.instance?.getDrawable(R.drawable.bg_common_btn_green)
                        } else {
                            AwBaseApplication.instance?.resources?.getColor(R.color.white)
                                ?.let { tv.setTextColor(it) }
                            tv.background = AwBaseApplication.instance?.getDrawable(R.drawable.bg_common_btn_gray)
                        }
                    } else {
                        AwBaseApplication.instance?.resources?.getColor(R.color.white)
                            ?.let { tv.setTextColor(it) }
                        tv.background =
                            AwBaseApplication.instance?.getDrawable(R.drawable.bg_common_btn_gray)
                    }
                }

            })
    }
}